<?php
namespace app\admin\controller;

use app\admin\model\Config;
use think\Request;
use think\Db;

class Index extends Common
{
    /**
     * @title   后台首页
     * @return mixed
     */
    public function index()
    {
        $list = db::name('admin_logs')->order('add_time desc')->paginate(10);
        $this->assign('list',$list);
        $this->assign('page',$list->render());
        return $this->fetch('admin/index/index');
    }

    /**
     * @title   网站配置信息
     * @return mixed
     */
    public function webSystem()
    {
        if(Request::instance()->isAjax()){
            $webconfig = json_encode(Request::instance()->param());
            $update = Db::name('system')->where('id',1)->update(['webconfig'=>$webconfig]);
            if($update){
                $this->success('更新成功');
            }else{
                $this->error('更新失败');
            }
        }else {
            return $this->fetch('admin/index/web_config');
        }
    }


    public function webWechat()
    {
        if(Request::instance()->isAjax()){
            $wechatconfig = json_encode(Request::instance()->param());
            $update = Db::name('system')->where('id',1)->update(['wechatconfig'=>$wechatconfig]);
            if($update){
                $this->success('更新成功');
            }else{
                $this->error('更新失败');
            }
        }else {
            return $this->fetch('admin/index/wechat_config');
        }
    }

    /**
     * @title   管理员上传头像
     */
    public function upload()
    {
        $file = request()->file('file');
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->move(ROOT_PATH . 'public/uploads');
        if($info){
            // 成功上传后 获取上传信息
            // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
            $result = array('state'=>'SUCCESS','saveName'=> $info->getSaveName());
            echo json_encode($result);
        }else{
            // 上传失败获取错误信息
            echo $file->getError();
        }
    }
}
